package com.example.myapplication5;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.viewpager.widget.PagerAdapter;
import java.util.List;

public class BannerAdapter extends PagerAdapter {
    private Context context;
    private List<Banner> bannerList;

    // 构造方法：传入上下文和数据
    public BannerAdapter(Context context, List<Banner> bannerList) {
        this.context = context;
        this.bannerList = bannerList;
    }

    @Override
    public int getCount() {
        return bannerList.size(); // 轮播图数量
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    // 创建每个页面
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // 加载轮播图item布局
        View view = LayoutInflater.from(context).inflate(R.layout.item_banner, null);
        ImageView ivBanner = view.findViewById(R.id.iv_banner);
        // 这里简化：用本地图片（实际项目用Glide加载网络图片）
        // 假设imageUrl存的是资源id字符串，转成int
        int resId = Integer.parseInt(bannerList.get(position).imageUrl);
        ivBanner.setImageResource(resId);
        // 添加到容器
        container.addView(view);
        return view;
    }

    // 销毁页面
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}